<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%@page import="com.webside.points.wechat.Wechat"%>
<%
	 //正常跳转
	String code = (String)request.getParameter("code");
  
	String openId = null;
	
	if(code != null){
		Wechat.getOpenId(code,request,response);
	}
	
	if(openId == null){
		openId = (String)request.getSession().getAttribute("openId");
	}
	//如果从其他界面返回时，code已经失效，需要重新请求一次
	
	if(openId == null){
	   response.sendRedirect("../wechat/getWeChatCode");
	   return;
	}  
	
 %>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <link rel="stylesheet" href="../css/amazeui.min.css">
  <link rel="stylesheet" href="../css/wap.css">
  <link rel="stylesheet" href="../css/style.css"/>
  <link rel="stylesheet" href="../css/weui.css" />
  <script src="../js/jquery.min.js"></script>
  <script type="text/javascript"
	src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    <script src="../js/jssdk.js"></script>
  <title>文章介绍</title>
  <style type="text/css">
  	.study{
  		border:1px solid #22ac38;
  		background: #22ac38;
  		border-radius:3px;
  		color:white;
  		font-size:14px;
  		padding:5px 8px;
  		width:38%;
  	}
  </style>
   <script type="text/javascript">
   $(function(){
	   var userId = $("#userId").val();
	   var postTypeId = $("#postTypeId").val();
	   var url ="${ctx}/userPost/findByUserId";
	   var params = {
			   userId:userId,
			   postTypeId:postTypeId
		};
	   $.post(url, params, function(data, status, xhr){
		   if(data != null){
			  if(data.postTypeId==postTypeId){
				$("#studyId").attr("href","${ctx }/post/findByPostTypeId?typeId="+postTypeId);
				$("#study").text("进入学习");
			} 
		   }	
		},'json'); 
   })
   
    var $loadingToast=null;
	
    function studyPay(amount){
    	var userId = $("#userId").val();
  	    var postTypeId = $("#postTypeId").val();
		$loadingToast = $('#loadingToast');
		if ($loadingToast.css('display') != 'none') return;
		$loadingToast.fadeIn(100);
		var url="${ctx }/wechat/getStudyOrder";
		var params = {
		  openId:'<%=openId%>',
		  amount:amount,
		  userId:userId,
		  postTypeId:postTypeId
		  
		};
		   
		 $.post(url, params, function(data, status, xhr){
			callbackCharge(data);
		},'json'); 
	}
  	
	 function callbackCharge(data) {
		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
						false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
				document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
			}
		} else {
			onBridgeReady(data);
		}
	}
	
	function onBridgeReady(data){
		 $loadingToast.fadeOut(100);
		   //alert(data.package);
		   WeixinJSBridge.invoke('getBrandWCPayRequest', 
		       {
		           "appId"		:data.appId,     //公众号名称，由商户传入     
		           "timeStamp"	:data.timeStamp, //时间戳，自1970年以来的秒数     
		           "nonceStr"	:data.nonceStr,  //随机串     
		           "package"	:data.package,     
		           "signType"	:data.signType,  //微信签名方式：     
		           "paySign"	:data.paySign    //微信签名 
		       },
		       function(res){ 
		       	   // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。   
		       	   //alert(res.err_msg);  
		       	   // $(".pet_hd_con_gp_list_xl_m").attr("onclick","payEvent()");
		           if(res.err_msg == "get_brand_wcpay_request:ok" ) {
		           		//alert("ok");
		           }else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
		           		//alert("cancel");
		           }else if(res.err_msg == "get_brand_wcpay_request:fail" ){
		           		//alert("fail");
		           }
		       }
		   ); 
		} 
  </script>
</head>
<body style="background:#ececec">
<div class="page">
    <div id="loadingToast" style="display:none;">
        <div class="weui-mask_transparent"></div>
        <div class="weui-toast">
            <i class="weui-loading weui-icon_toast"></i>
            <p class="weui-toast__content">数据加载中</p>
        </div>
    </div>
</div>
<input type="hidden" value="${user.id}" id="userId">
<input type="hidden" value="${postType.id}" id="postTypeId">
  <div class="pet_mian" >
    <div class="pet_content pet_content_list">
      <div class="pet_grzx">
          <div class="pet_grzx_nr" style="padding: 0px 20px 0;">
              <div class="pet_grzx_ico" style="width:100px;height:150px;border-radius: 0;">
                <img src="${postType.image }" >
              </div>
              <div class="pet_grzx_num_font" style="text-align:left;text-indent:2em;">
			  ${postType.intro}
              </div>
              <div class="pet_grzx_num" style="padding:30px 15px;">
                 <a href="${ctx }/post/findByPostId?id=${post.id}">
					<span class="study" style="color: white;width: 38%;">免费试读</span>
				 </a>
				<!-- <a  href="${ctx }/post/findByPostTypeId?typeId=1">-->
				
				 
				 <c:if test="${postType.price > 0}">
					 <a href="#" id="studyId">
						<span class="study" id="study" style="float:right;color: white;width: 38%;" onclick="studyPay(${postType.price})">￥${postType.price} 学习</span>
					 </a>
				 </c:if>
				 <c:if test="${postType.price <= 0}">
					 <a  href="${ctx }/post/findByPostTypeId?typeId=${postType.id}">
						<span class="study" id="study" style="float:right;color: white;width: 38%;" onclick="studyPay(${postType.price})">￥${postType.price} 学习</span>
					 </a>
				 </c:if>
              </div>           
          </div>
		  <div class="pet_grzx_list" style="border-top: 10px solid #f0f0f0;">
			 <div class="pet_grzx_num_font" style="text-align:left;padding-top: 0px;color:#333;font-size:16px;text-indent:2em;">
			  ${postType.describe}
              </div>
		  </div>
        </div>

      </div>
 <!--底部-->
    <div data-am-widget="navbar" class="am-navbar am-cf my-nav-footer " id="">
     <ul class="am-navbar-nav am-cf am-avg-sm-4 my-footer-ul">
		<li>
           <a href="${ctx }/user/findUserIssue?issueTypeId=0" class="">
            <span class="am-icon-home"></span>
            <span class="am-navbar-label">首页</span>
          </a>
        </li>
        <li>
          <a href="${ctx }/issueType/findAll?mark=service&openId=<%=openId %>" class="">
            <span class="am-icon-comments"></span>
            <span class="am-navbar-label">提问</span>
          </a>
        </li>
		<li>
          <a href="${ctx }/postType/findAllPostType" class="">
            <span class="am-icon-book"></span>
            <span class="am-navbar-label">学习</span>
          </a>
        </li>
        <li style="position:relative">
          <a href="${ctx }/user/findByOpenId?openId=<%=openId %>" class="">
            <span class="am-icon-user"></span>
            <span class="am-navbar-label">我的</span>
          </a>
        </li>
      </ul>
</div>
    </div>
  <script src="../js/amazeui.min.js"></script>
    <script src="../js/text-calculation.js"></script>
</body>
  </html>